package com.xiaotu.storage;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import com.xiaotu.storage.model.CacheDataModel;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @类名 StarAwardsStorageProcessor
 * @日期 2017年10月09日
 * @作者 高海军
 * @功能 艺人获奖数据转储实现类
 */
@Component("StarAwardsStorageProcessor")
public class StarAwardsStorageProcessor extends AHSetStorageProcessor
{
    private static final String[] FIELD_ARR = new String[]{"siteName", "name", "awardType",
            "awardSection", "awardTitle", "awardWork", "awardRole", "awardTime", "awardMode",
            "timestamp"};

    private static final String[] CONVERT_ARR = new String[]{AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.TIMESTAMP_CONVERT};

    private static final boolean[] THROW_ARR = new boolean[]{true, true, true, true, true, true,
            true, true, true, true};

    @Override
    protected String getRedisKey(Date date)
    {
        return DataRedisKeyUtils.getKey(Constants.DataRedisKey.MaoYan_Awards_YYYYMMDD, date);
    }

    @Override
    public String process(JSONObject target, CacheDataModel cacheData, Date date)
    {
        this.validateAllNotEmpty(target, FIELD_ARR[0], FIELD_ARR[1], FIELD_ARR[2], FIELD_ARR[3],
                FIELD_ARR[4]);
        this.setFormatedField(target, "awardTime", RegexUtils.REGEX_CH);
        this.setFormatedField(target, "awardRole", "饰：");
        if (target.has("awardMode"))
        {
            switch (target.getString("awardMode"))
            {
                case "提名":
                    target.put("awardMode", 1);
                    break;
                case "获奖":
                    target.put("awardMode", 0);
                    break;
                default:
                    throw new SpiderException(SpiderExceptionCode.DataInvalideException,
                            "awardMode字段错误[" + target.getString("awardMode") + "]");
            }
        }
        return this.list2CSVLine(ConverterFactory.convert(target, FIELD_ARR, CONVERT_ARR,
                THROW_ARR), SepratorUtil.SEP_PIPE);
    }

    private void setFormatedField(JSONObject target, String field, String replace)
    {
        if (target.has(field) && target.getString(field) != null)
            target.put(field, RegexUtils.replaceStr(target.getString(field), replace, StringUtils
                    .EMPTY));
    }

    @Override
    public String getDataType()
    {
        return Constants.PageDataType.STAR_RECORD;
    }

    @Override
    public String getTaskType()
    {
        return "StarAwards";
    }
}
